﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Gizmox.WebGUI.Forms;
using System.Xml;

namespace PLACEHR.Web.Utilities
{
    public class LanLayer
    {
        static readonly Dictionary<int, ContentInfo> _contentDic;
        static LanLayer()
        {
            _contentDic = new Dictionary<int, ContentInfo>();
        }

        public static string GetTrans(int contentId)
        {
            return GetTrans(contentId, Global.LanguageCode);
        }

        public static string GetTrans(int contentId, string lanCode)
        {
            string result = string.Empty;
            ContentInfo contentInfo = null;
            if (_contentDic.ContainsKey(contentId))
            {
                contentInfo =  _contentDic[contentId];
            }
            else
            {
                contentInfo = LoadContentInfo(contentId);
                if (contentInfo != null)
                {
                    _contentDic.Add(contentId, contentInfo);
                }
            }

            if (contentInfo != null)
            {
                result = string.Equals(lanCode, Constants.EnCode) ? contentInfo.EnglishTrans : contentInfo.ChineseTrans;
            }
            else
            {
                result = string.Format("{0}:null", contentId);
            }

            return result;
        }

        public static bool RefreshItem(int contentId)
        {
            bool result = false;
            if (_contentDic.ContainsKey(contentId))
            {
                _contentDic.Remove(contentId);
                result = true;
            }

            return result;
        }

        /// <summary>
        /// Load content translation from database
        /// </summary>
        /// <param name="contentId"></param>
        /// <returns></returns>
        private static ContentInfo LoadContentInfo(int contentId)
        {
            ContentInfo contentInfo = null;

            try
            {
                var content = Global.Context.Contents.FirstOrDefault(p => p.ContentId == contentId);
                if (content != null)
                {
                    contentInfo = new ContentInfo
                    {
                        ContentID = contentId,
                        DefaultValue = content.DefaultValue,
                    };
                    var csTrans = content.ContentTranslations.FirstOrDefault(p => string.Equals(p.LanguageCode.Trim(), Constants.ZhCode, StringComparison.OrdinalIgnoreCase));
                    var enTrans = content.ContentTranslations.FirstOrDefault(p => string.Equals(p.LanguageCode.Trim(), Constants.EnCode, StringComparison.OrdinalIgnoreCase));
                    contentInfo.ChineseTrans = csTrans == null ? string.Empty : csTrans.Translation;
                    contentInfo.EnglishTrans = enTrans == null ? string.Empty : enTrans.Translation;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return contentInfo;
        }

    }

    public class ContentInfo
    {
        public int ContentID { get; set; }
        public string DefaultValue { get; set; }
        public string ChineseTrans { get; set; }
        public string EnglishTrans { get; set; }
    }

}